import java.io.File;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author fox
 */
public class OJMDumperInterface extends javax.swing.JFrame {

    File selected_file;
    File output_dir;

    /** Creates new form OJMDumper */
    public OJMDumperInterface() {
        output_dir = new File(System.getProperty("user.dir"));
        initComponents();
        lbl_outdir.setText(output_dir.getPath());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bt_openfile = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbl_filepath = new javax.swing.JLabel();
        lbl_filetype = new javax.swing.JLabel();
        bt_dump = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        lbl_outdir = new javax.swing.JLabel();
        bt_changedir = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        bt_openfile.setText("Open file");
        bt_openfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_openfileActionPerformed(evt);
            }
        });

        jLabel1.setText("OJM Dumper by Fox");

        jLabel2.setText("open2jam.wordpress.com");

        lbl_filepath.setText("jLabel3");

        lbl_filetype.setText("jLabel3");

        bt_dump.setText("Dump Sound");
        bt_dump.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_dumpActionPerformed(evt);
            }
        });

        jLabel3.setText("Output dir:");

        lbl_outdir.setText("jLabel4");

        bt_changedir.setText("Change output dir");
        bt_changedir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_changedirActionPerformed(evt);
            }
        });

        jLabel4.setText("File path:");

        jLabel5.setText("File type:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_openfile)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_changedir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_dump))
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbl_outdir)))
                .addContainerGap(49, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lbl_filepath))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(lbl_filetype)))
                .addContainerGap(276, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(233, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_openfile)
                    .addComponent(bt_changedir)
                    .addComponent(bt_dump))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_filepath)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbl_filetype)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbl_outdir))
                .addGap(45, 45, 45)
                .addComponent(jLabel2)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_openfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_openfileActionPerformed

        JFileChooser jfc = new JFileChooser();
        if(selected_file !=null)jfc.setCurrentDirectory(selected_file.getParentFile());
        else jfc.setCurrentDirectory(output_dir);
        jfc.setDialogTitle("Choose a OJM file");
        jfc.addChoosableFileFilter(new FileFilter(){
            public boolean accept(File f) {
                if(f.isDirectory())return true;
                if(f.getName().toLowerCase().endsWith(".ojm"))return true;
                return false;
            }
            @Override
            public String getDescription() { return "";}
        });
        jfc.setAcceptAllFileFilterUsed(false);
        if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File f = jfc.getSelectedFile();
            if(f.isDirectory()){
                JOptionPane.showMessageDialog(this, "You must choose one OJM file");
                return;
            }
            selected_file = f;
            updateSelection();
        }
    }//GEN-LAST:event_bt_openfileActionPerformed

    private void bt_changedirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_changedirActionPerformed

        JFileChooser jfc = new JFileChooser();
        jfc.setCurrentDirectory(output_dir);
        jfc.setDialogTitle("Choose a directory");
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.setAcceptAllFileFilterUsed(false);
        if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            output_dir = jfc.getSelectedFile();
            lbl_outdir.setText(output_dir.getPath());
        }
    }//GEN-LAST:event_bt_changedirActionPerformed

    private void bt_dumpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_dumpActionPerformed

        OJMDumper.dumpFile(selected_file, output_dir);
        JOptionPane.showMessageDialog(this, "Dump finished !");
    }//GEN-LAST:event_bt_dumpActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        trySetLAF();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OJMDumperInterface().setVisible(true);
            }
        });
    }

    private static void trySetLAF()
    {
        try {
            for(UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
            {
                if("Nimbus".equals(info.getName())){
                    UIManager.setLookAndFeel(info.getClassName());
                    return;
                }
            }
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {}
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_changedir;
    private javax.swing.JButton bt_dump;
    private javax.swing.JButton bt_openfile;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lbl_filepath;
    private javax.swing.JLabel lbl_filetype;
    private javax.swing.JLabel lbl_outdir;
    // End of variables declaration//GEN-END:variables

    private void updateSelection() {
        lbl_filepath.setText(selected_file.getPath());
        try {
            lbl_filetype.setText(OJMDumper.getType(selected_file));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "An IOException occured !");
            return;
        }
        
    }
}
